package 网络编程.UDP协议;

import java.io.IOException;
import java.net.*;

public class UDP发送数据 {
    public static void main(String[] args) throws IOException {
        //发送数据

        //1.创建发送端对象DatagramSocket
        DatagramSocket ds=new DatagramSocket();

        //2.打包数据DatagramPacket对象,对数据进行打包
        String str="你好,第er条";
        byte[] bytes = str.getBytes();
        //指定要发送数据的主机
        InetAddress address=InetAddress.getByName("127.0.0.1");
        //指定端口号
        int port=10086;
        DatagramPacket dp = new DatagramPacket(bytes,bytes.length,address,port);

        //3.发送数据
        ds.send(dp);

        //4.释放资源
        ds.close();
    }
}
